package com.turing.sensivitycode.service;

import com.turing.common.entity.Context;
import com.turing.common.entity.UserToken;
import com.turing.common.util.StringUtils;
import com.turing.sensivitycode.domain.SensivityCode;
import com.turing.sensivitycode.domain.SensivityCodeExample;
import com.turing.sensivitycode.domain.SensivityLexcion;
import com.turing.sensivitycode.domain.SensivityLexcionExample;
import com.turing.sensivitycode.mapper.SensivityCodeMapper;
import com.turing.sensivitycode.mapper.SensivityLexcionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
public class SensivityNameService {

    private static final Logger logger = LoggerFactory.getLogger(SensivityNameService.class);


    @Autowired
    private SensivityLexcionMapper sensivityLexcionMapper;

    @Autowired
    private SensivityCodeMapper sensivityCodeMapper;

    // 当前默认是从Excel文件中来处理对应的文件
    public Object processUploadfile(MultipartFile[] files) {

        return null;
    }

    public String createSensivityLexcion(String lexcionName,String description) {

        if (StringUtils.isEmpty(lexcionName)){
            String msg = "敏感词库为空！请检查";
            logger.error(msg);
            throw new IllegalArgumentException(msg);
        }
        UserToken tokenContext = Context.getTokenContext();
        // 创建人ID
        Integer userId = tokenContext.getUserId();
        String userName = tokenContext.getUserName();
        if (lexcionName.contains(";")){
            String[] split = lexcionName.split(";");
            int i = 0;
            for (String sensivityName : split) {
                SensivityLexcion sensivityLexcion = new SensivityLexcion();
                sensivityLexcion.setCreatedBy(userName);
                sensivityLexcion.setSensivityname(sensivityName);
                sensivityLexcion.setCreatedTime(new Date());
                sensivityLexcion.setDescription(description);
                sensivityLexcion.setVersion(0);
                sensivityLexcion.setIsDel(0);
                sensivityLexcionMapper.insert(sensivityLexcion);
                i++;
            }
            logger.info("插入成功,当前插入进去的条目有：{}",i);
            return "敏感词库新增成功";
        }

        if (lexcionName.contains("；")){
            String[] split = lexcionName.split("；");
            for (String sensivityName : split) {
                SensivityLexcion sensivityLexcion = new SensivityLexcion();
                sensivityLexcion.setCreatedBy(userName);
                sensivityLexcion.setSensivityname(sensivityName);
                sensivityLexcion.setCreatedTime(new Date());
                sensivityLexcion.setDescription(description);
                sensivityLexcion.setVersion(0);
                sensivityLexcion.setIsDel(0);
                sensivityLexcionMapper.insert(sensivityLexcion);
            }
            return "敏感词库新增成功";
        }
        return "敏感词库新增失败";
    }


    public List<SensivityLexcion> getAllSensivityLexcion() {
        SensivityLexcionExample sensivityLexcionExample = new SensivityLexcionExample();
        sensivityLexcionExample.createCriteria().andIsDelEqualTo(0);
        return sensivityLexcionMapper.selectByExample(sensivityLexcionExample);
    }

    public String deleteByLexcionID(Integer lexcionID) {
        SensivityLexcionExample sensivityLexcionExample = new SensivityLexcionExample();
        sensivityLexcionExample.createCriteria().andIsDelEqualTo(0).andIdEqualTo(lexcionID);
        SensivityLexcion sensivityLexcion = new SensivityLexcion();
        sensivityLexcion.setIsDel(1);
        int i = sensivityLexcionMapper.updateByExampleSelective(sensivityLexcion, sensivityLexcionExample);
        logger.info("一共更新了{}个敏感词库",i);
        // 同时需要更新到敏感词中
        SensivityCode sensivityCode = new SensivityCode();
        sensivityCode.setIsDel(1);
        SensivityCodeExample sensivityCodeExample = new SensivityCodeExample();
        sensivityCodeExample.createCriteria().andLexcitionIdEqualTo(lexcionID);
        int updateCount = sensivityCodeMapper.updateByExampleSelective(sensivityCode, sensivityCodeExample);
        logger.info("更新了敏感词库中的敏感词的数量为：{}",updateCount);
        return "SUCCESS";
    }
}
